/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.KeyResponse;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.repository.KeyResponseRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author ronildo
 */
public class KeyResponseDAO extends GenericDAO<KeyResponse, KeyResponseRepository> implements KeyResponseRepository {

    private static final Logger logger = LoggerFactory.getLogger();

    public KeyResponse findById(Long id) {
        try{
            return (KeyResponse)em.createNamedQuery("KeyResponse.findByPkKeyResponse").
                setParameter("pkKeyResponse", id).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.INFO, "Cannot find KeyResponse for this id: " + id);
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public KeyResponse findByName(String name) {
        try{
            return (KeyResponse)em.createNamedQuery("KeyResponse.findByName").
                setParameter("name", name).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.INFO, "Cannot find KeyResponse for this name: " + name);
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public KeyResponse findByNameWithoutCasesensitive(String name) {
        try{
            return (KeyResponse)em.createQuery(" SELECT k FROM KeyResponse k WHERE lower(k.name) = :name").
                setParameter("name", name.toLowerCase()).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.INFO, "Cannot find KeyResponse for this name: " + name);
            return null;
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public List<KeyResponse> list(KeyResponse keyResponse){
        try{
            Login login = keyResponse.getLogin();
            String hql = "select keyResponse from KeyResponse as keyResponse ";
            hql+=" left outer join keyResponse.login as login";
            hql+=" where keyResponse.pkKeyResponse = keyResponse.pkKeyResponse";

            if(keyResponse.getName() != null && !keyResponse.getName().equals(""))
                hql+=" and keyResponse.name like :keyResponse_name";
            if(keyResponse.getTip() != null && !keyResponse.getTip().equals(""))
                hql+=" and keyResponse.tip like :keyResponse_tip";
            if(login != null && login.getPkLogin() != null)
                hql+=" and login.pkLogin = :pkLogin";
            hql+=" order by keyResponse.name";

            Query query = em.createQuery(hql);
            if(keyResponse.getName() != null && !keyResponse.getName().equals(""))
                query.setParameter("keyResponse_name", keyResponse.getName()+"%");
            if(keyResponse.getTip() != null && !keyResponse.getTip().equals(""))
                query.setParameter("keyResponse_tip", keyResponse.getTip()+"%");
            if(login != null && login.getPkLogin() != null)
                query.setParameter("pkLogin", login.getPkLogin());

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list key response",e);
            throw new DAOException(e);
        }
    }

    public List<KeyResponse> listAll(){
        try{
            return em.createNamedQuery("KeyResponse.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }
}
